package com.ez.supervise.model.dto;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import com.ez.supervise.model.sys.Role;
import com.ez.supervise.model.sys.UserRole;

public class UserCheckRole extends Role {

    private static final long serialVersionUID = 1692964570733280364L;
    
    private boolean checked;

    public UserCheckRole(boolean checked) {
	super();
	this.checked = checked;
    }
    public UserCheckRole() {
	super();
    }
    public void setChecked(boolean checked) {
	this.checked = checked;
    }

    public boolean isChecked() {
	return checked;
    }
    public static UserCheckRole build(Role role, boolean checked){
	UserCheckRole ucr = new UserCheckRole(checked);
	try {
	    org.apache.commons.beanutils.BeanUtils.copyProperties(ucr, role);
	} catch (IllegalAccessException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (InvocationTargetException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	return ucr;
    }
    
    public static List<UserCheckRole> build(List<Role> roles, List<UserRole> userRoles){
	List<UserCheckRole>  result = new ArrayList<UserCheckRole>();
	boolean checked = false;
	for (Role role : roles) {
	    if(role != null) {
		checked = checked(userRoles, role.getId());
		result.add(build(role,checked));
	    }
	}
	return result;
    }
    
    public static boolean checked(List<UserRole> userRoles, Long roleId){
	for (UserRole ur : userRoles) {
	    if(ur != null && ur.getRoleId().equals(roleId)){
		return true;
	    }
	}
	return false;
    }

}
